import 'package:dio/dio.dart';
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nft/api/API.dart';
import 'package:flutter_nft/page/home/detail/digital_detail.dart';
import 'package:flutter_nft/page/home/hometable/content_selection.dart';
import 'package:flutter_nft/page/home/hometable/digital_collection.dart';
import 'package:flutter_nft/page/home/hometable/launch_calendar.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class FindIndex extends StatelessWidget {
  const FindIndex({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MyHomePage(title: '首页');
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({Key? key, required this.title}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  late TabController _tabC;
  var tabs = ["美妆", "生活用品"];
  List<Map> imageMap = [];

  getBannerList()async{
    var request = await new Dio().get(API.reqUrl+"/get/banner/list");

    setState(() {
      imageMap = [
        {"id": 1, "url": request.data["dataObj"][1]["bannerUrl"].toString()},
        {"id": 2, "url": request.data["dataObj"][2]["bannerUrl"].toString()},
      ];
    });
  }

  List<Map> titleTab = [
    {"id": 0, "name": "数字"},
    {"id": 1, "name": "发售日历"},
    {"id": 2, "name": "内容精选"},
  ];

  int titleIndex = 0;

  List<Widget> widgetList = [
    DigitalCollection(),
    LaunchCelendar(),
    ContentSelection(),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController();
    getBannerList();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: _contentView(context, widget.title));
  }

  _contentView(context, title) {
    double statusBarHeight = MediaQuery.of(context).padding.top;
    double pinnedHeaderHeight = statusBarHeight + kToolbarHeight;
    return ExtendedNestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return <Widget>[
          //标题
          SliverAppBar(
            elevation: 0,
            leading: Container(
              //color: Colors.red,
              alignment: Alignment.bottomLeft,
              margin: EdgeInsets.only(left: 10, bottom: 2),
              width: 130,
              height: 70,
              child: Text(
                "鲸探",
                style: TextStyle(fontSize: 22, fontWeight: FontWeight.w700),
              ),
            ),
            actions: [
              Container(
                alignment: Alignment.bottomRight,
                margin: EdgeInsets.only(right: 10, bottom: 2),
                width: 50,
                height: 50,
                child: Icon(Icons.visibility_rounded),
              )
            ],
            pinned: true,
            floating: true,
            backgroundColor: Color.fromRGBO(50, 55, 58, 1),
            //backgroundColor: Colors.blue,
          ),

          SliverToBoxAdapter(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  //margin: EdgeInsets.only(left: 10, right: 10),
                  decoration:
                      BoxDecoration(color:  Color.fromRGBO(50, 55, 58, 1)),
                  height: 160,
                  child: new Swiper(
                    itemBuilder: (BuildContext context, int index) {
                      print(index);
                      return new InkWell(
                        hoverColor: Colors.deepPurple,
                        focusColor: Colors.deepPurple,
                        onTap: () {
                          Navigator.of(context).push(MaterialPageRoute(
                            builder: (context) {
                              return DigitalDetail();
                            },
                            //RouteSettings里面可以修改路由的名称 参数
                            settings: RouteSettings(
                              arguments: {"id": "2345676"},
                            ),
                            //  释放没有用的路由内存
                            maintainState: false,
                            fullscreenDialog: false,
                          ));

                          print("打印。。跳转。。" + index.toString());
                        },
                        child: Image.network(
                          imageMap[index]["url"].toString(),
                          // fit: BoxFit.fill,
                          width: 100,
                        ),
                      );
                    },
                    containerHeight: 10,
                    //autoplay: true,
                    itemCount: imageMap.length,
                    //指示器样式
                    pagination:
                        new SwiperPagination(margin: EdgeInsets.only(bottom: 20)),
                    //左右箭头
                    //control: new SwiperControl(),
                  ),
                ),
              ],
            ),
          ),
        ];
      },
      pinnedHeaderSliverHeightBuilder: () {
        return pinnedHeaderHeight;
      },
      onlyOneScrollInBody: true,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Color.fromRGBO(50, 55, 58, 1),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _tabars(),
            widgetList[titleIndex],
            //_tarbarview(),
          ],
        ),
      ),
    );
  }

  //tabars设置
  _tabars() {
    return Padding(
      padding: EdgeInsets.only(bottom: 15),
      child: Container(
        width: double.infinity,
        height: 45,
        color: Color.fromRGBO(50, 55, 58, 1),
        child: Row(
          children: titleTab
              .map(
                (e) => InkWell(
                  onTap: () {
                    setState(() {
                      titleIndex = e["id"];
                    });
                    print(e["id"].toString());
                  },
                  child: Container(
                    color: Color.fromRGBO(50, 55, 58, 1),
                    margin: EdgeInsets.only(left: 10, top: 13),
                    child: Text(
                      e["name"].toString(),
                      style: e["id"] == titleIndex
                          ? TextStyle(
                              color: Colors.white,
                              fontSize: 22,
                              fontWeight: FontWeight.w700)
                          : TextStyle(
                              fontSize: 20,
                              color: Color.fromRGBO(166, 168, 168, 1),
                              fontWeight: FontWeight.w600),
                    ),
                  ),
                ),
              )
              .toList(),
        ),
      ),
    );
  }

  void _tabController() {
    _tabC = TabController(
      length: tabs.length,
      initialIndex: 0,
      vsync: this,
    );
  }
}
